package cn.kongwg.demo02;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @author kongwg
 * @create 2021-05-25 15:03
 */
public class AtomicDemo {
    int number = 0;

    public void addpp() {
        number++;
    }

    AtomicInteger atomicInteger = new AtomicInteger();

    public void atomicAdd(){
        atomicInteger.getAndIncrement();
        //atomicInteger.compareAndSet()
    }

    public static void main(String[] args) {
        AtomicDemo atomicDemo = new AtomicDemo();
        for (int i = 0; i < 20; i++) {
            new Thread(() -> {
                for (int j = 0; j < 1000; j++) {

                    atomicDemo.addpp();
                    atomicDemo.atomicAdd();
                }
            }, String.valueOf(i)).start();

        }


        while (Thread.activeCount() > 2) {
            Thread.yield();
        }
        System.out.println(Thread.currentThread().getName() + "\t " + atomicDemo.number);
        System.out.println(Thread.currentThread().getName() + "\t " + atomicDemo.atomicInteger);


    }


}
